package com.mxw.controller;

import com.mxw.entity.UserInfo;
import com.mxw.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

// 注册 recommend

@Controller
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/login")
    public String loginPage() {
        return "login";
    }

    @GetMapping("/register")
    public String registerPage() {
        return "register";
    }



    @GetMapping("/contact")
    public String contactPage() {
        return "contact";
    }

    @PostMapping("/api/register")
    @ResponseBody
    public ResponseEntity<?> register(@RequestBody UserInfo userInfo) {
        Map<String, Object> response = new HashMap<>();
        
        // 参数校验
        if (userInfo == null || userInfo.getUserName() == null || userInfo.getPassword() == null || userInfo.getEmail() == null) {
            response.put("success", false);
            response.put("message", "请填写完整的注册信息");
            return ResponseEntity.badRequest().body(response);
        }

        // 检查用户名是否已存在
        if (userService.isUserNameExists(userInfo.getUserName())) {
            response.put("success", false);
            response.put("message", "用户名已存在");
            return ResponseEntity.badRequest().body(response);
        }

        // 检查邮箱是否已存在
        if (userService.isEmailExists(userInfo.getEmail())) {
            response.put("success", false);
            response.put("message", "邮箱已被注册");
            return ResponseEntity.badRequest().body(response);
        }

        // 注册用户
        UserInfo registeredUser = userService.register(userInfo);
        if (registeredUser != null) {
            response.put("success", true);
            response.put("message", "注册成功");
            response.put("data", registeredUser);
            return ResponseEntity.ok(response);
        } else {
            response.put("success", false);
            response.put("message", "注册失败，请稍后重试");
            return ResponseEntity.badRequest().body(response);
        }
    }

    @PostMapping("/api/login")
    @ResponseBody
    public ResponseEntity<?> login(@RequestBody Map<String, String> loginRequest, HttpSession session) {
        Map<String, Object> response = new HashMap<>();
        
        String userName = loginRequest.get("userName");
        String password = loginRequest.get("password");

        // 参数校验
        if (userName == null || password == null) {
            response.put("success", false);
            response.put("message", "请填写用户名和密码");
            return ResponseEntity.badRequest().body(response);
        }

        // 登录验证
        UserInfo userInfo = userService.login(userName, password);
        if (userInfo != null) {
            // 将用户信息存入session
            session.setAttribute("user", userInfo);
            response.put("success", true);
            response.put("message", "登录成功");
            response.put("data", userInfo);
            return ResponseEntity.ok(response);
        } else {
            response.put("success", false);
            response.put("message", "用户名或密码错误");
            return ResponseEntity.badRequest().body(response);
        }
    }

    @GetMapping("/api/logout")
    @ResponseBody
    public ResponseEntity<?> logout(HttpSession session) {
        session.removeAttribute("user");
        Map<String, Object> response = new HashMap<>();
        response.put("success", true);
        response.put("message", "退出成功");
        return ResponseEntity.ok(response);
    }
}
